﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;

namespace Coolzon.UI.Core.Properties
{
    /// <summary>
    /// 调色板
    /// </summary>
    public class BrushList
    {
        private static Brush[] s_brushs = new Brush[]{
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF6F06")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFA4D838")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF0000")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00FF00")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF0000FF")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFFFF00")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF00FF")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF00FFFF")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF0000FF")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF8DB143")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF871F78")),
            new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFCD7F32"))
        };

        private List<Brush> _brushList;

        public BrushList()
        {
            _brushList = new List<Brush>();
        }

        public int Count
        {
            get
            {
                return _brushList.Count;
            }
        }

        public Brush this[int index]
        {
            get
            {
                return Get(index);
            }
            set
            {
                _brushList[index] = value;
            }
        }

        /// <summary>
        /// 从调色板中获取颜色，如果没有则返回一个随机颜色
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private Brush Get(int index)
        {
            if (this._brushList == null)
            {
                if (index < s_brushs.Length)
                    return s_brushs[index];
                return new SolidColorBrush(Color.FromArgb(255, (byte)new Random().Next(0, 255), (byte)new Random().Next(0, 255), (byte)new Random().Next(0, 255)));
            }
            else if(this._brushList.Count <= index)
            {
                int delta = index - this._brushList.Count;
                if (delta < s_brushs.Length)
                    return s_brushs[delta];
                return new SolidColorBrush(Color.FromArgb(255, (byte)new Random().Next(0, 255), (byte)new Random().Next(0, 255), (byte)new Random().Next(0, 255)));
            }
            return this._brushList[index];
        }

        /// <summary>
        /// 添加颜色
        /// </summary>
        /// <param name="brush"></param>
        public void Add(Brush brush)
        {
            this._brushList.Add(brush);
        }
    }
}
